﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace BL.BetterControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:_RadioButton Description=\"\" DivStyle=\"\"  DivType=\"\" LabelClass=\"\" runat=\"server\"></{0}:_RadioButton>")]
    public class _RadioButton : RadioButton
    {
        public string DivType { get { return ViewState["DivType"] != null ? (string)ViewState["DivType"] != "" ? (string)ViewState["DivType"] : "none" : "none"; } set { ViewState["DivType"] = value; } }
        public string DivStyle { get { return ViewState["DivStyle"] != null ? (string)ViewState["DivStyle"] : ""; } set { ViewState["DivStyle"] = value; } }
        /// <summary>
        /// Sätt LabelClass = none för att inte skriva ut någon label
        /// </summary>
        public string LabelClass { get { return ViewState["LabelClass"] != null ? (string)ViewState["LabelClass"] : ""; } set { ViewState["LabelClass"] = value; } }
        public string Description { get { return ViewState["Description"] != null ? (string)ViewState["Description"] : ""; } set { ViewState["Description"] = value; } }
        
        /// <summary>
        /// Gör så att GroupName fungerar för kontroller på olika rader i tex en listview där de annars skulle få unika groupname / rad 
        /// </summary>
        public bool KeepGroupName { get { return ViewState["KeepGroupName"] != null ? (bool)ViewState["KeepGroupName"] : false; } set { ViewState["KeepGroupName"] = value; } }
        /// <summary>
        /// .net skapar som default en <span> runt radiobutton där den lägger CssClass och ToolTip  AddSpanTag = false tar bort det och lägger tillbaka attributen på radiobutton
        /// </summary>
        public bool AddSpanTag { get { return ViewState["AddSpanTag"] != null ? (bool)ViewState["AddSpanTag"] : false; } set { ViewState["AddSpanTag"] = value; } }

        public override string UniqueID
        {
            get
            {
                string uid = base.UniqueID;
                //Fix groupname in lisview
                //if (KeepGroupName && this.Parent is ListViewDataItem && GroupName != "")
                if (KeepGroupName && GroupName != "" && this.Parent != null)
                {
                    uid = this.Parent.UniqueID;
                    uid = uid.Remove(uid.LastIndexOf('$'));
                    uid += "$" + GroupName;

                }
                return uid;


            }
        }

        protected override void Render(HtmlTextWriter writer)
        {

            if (this.ToolTip == "")
                this.ToolTip = this.Description;

            if (DivType.ToLower() != "none")
            {
                writer.Write("<div");
                if (DivType != string.Empty)
                    writer.Write(" class=\"" + DivType + "\"");
                if (DivStyle != string.Empty)
                    writer.Write(" style=\"" + DivStyle + "\"");

                writer.Write(">");

            }

            if (LabelClass.ToLower() != "none")
            {
                string labelDisable = "";

                if (!this.Enabled)
                    labelDisable = "disabled";


                if (LabelClass != String.Empty)
                    writer.Write("<label class=\"" + LabelClass + "\" " + labelDisable + " for=\"" + this.ClientID + "\" title=\"" + this.ToolTip + "\">" + this.Description + "</label>");
                else
                    writer.Write("<label for=\"" + this.ClientID + "\" " + labelDisable + " title=\"" + this.ToolTip + "\">" + this.Description + "</label>");

            }



            if (!CssClass.Contains("radioButton"))
            {
                if (CssClass.Length > 0)
                    CssClass = CssClass + " radioButton";
                else
                    CssClass = "radioButton";
            }

            //.net skapar som default en <span> runt radiobutton där den lägger CssClass och ToolTip  AddSpanTag = false tar bort det och lägger tillbaka attributen på radiobutton
            if (AddSpanTag)
            {
                base.Render(writer);
            }
            else
            {
                StringWriter w = new StringWriter();
                HtmlTextWriter rbw = new HtmlTextWriter(w);
                base.Render(rbw);
                rbw.Close();                
                string html = w.GetStringBuilder().ToString();
                if (html.Contains("<span"))
                {

                    int start = html.IndexOf("<input");
                    int end = html.IndexOf("/>", start);
                    string rbHtml = html.Substring(start, (end - start));

                    if (CssClass != "")
                        rbHtml = rbHtml + " class=\"" + CssClass + "\"";

                    if (ToolTip != "")
                        rbHtml = rbHtml + " title=\"" + ToolTip + "\"";

                        html = rbHtml + "/>";                    
                }


                writer.Write(html);
            }
           


            if (DivType.ToLower() != "none")
                writer.Write("</div>"); ;

        }
    }
}
